/* Version 0.01 2012.06.27  */

#include "displayable.h"

void TextInfo::setPos (float _x, float _y)
{
	x = _x;
	y = _y;
}

void TextInfo::updatePos (int ox, int oy)
{
	TextOverlay::SetPos ((float)ox+x, (float)oy+y);
}

void ImageInfo::setImage(const char* mtr_name)
{
	pBackImg->setMaterialName(mtr_name);
}


void ImageInfo::load(Ogre::Overlay* pOverlay, const char* mtr_name, int tx, int ty, int w, int h)
{
	x = tx;
	y = ty;

	Ogre::OverlayManager& overlayManager = Ogre::OverlayManager::getSingleton();

	static int in = 0;
	in ++;
	char buf[256];
	sprintf(buf, "image info %d", in);

	pBackImg = (Ogre::OverlayContainer*)overlayManager.createOverlayElement("Panel", buf);

	pBackImg->setMetricsMode(Ogre::GMM_PIXELS);
	pBackImg->setPosition((float)x, (float)y);
	pBackImg->setDimensions((float)w, (float)h);
	pBackImg->setMaterialName(mtr_name);
	pBackImg->show();

	pOverlay->add2D(pBackImg);
}

void ImageInfo::updatePos(int ox, int oy)
{
	pBackImg->setPosition((float)ox, (float)oy);
}

void ImageInfo::show(bool b)
{
	if (b)
		pBackImg->show();
	else
		pBackImg->hide();
}

Displayable::Displayable(float _x, float _y, int _w, int _h)
:x(_x), y(_y), w(_w), h(_h), bCanDrag(FALSE), bCheck(FALSE), bClick(FALSE), pOverlay(false)
{

}

void Displayable::setZOrder(int i)
{
	if(pOverlay != NULL)
		pOverlay->setZOrder(i);
}

TextOverlay* Displayable::addText (const char* txt, float x, float y, float r, float g, float b)
{
	if(pOverlay != NULL)
	{
		TextInfo tmp;
		vText.push_back(tmp);

		vText.back().Init(pOverlay);
		vText.back().setPos(x, y);
		vText.back().SetText(txt, r, g, b);
		return &(vText.back());
	}

	return NULL;
}

void  Displayable::clearText()
{
	if(pOverlay != NULL)
	{
		V_TEXT::iterator pt = vText.begin();
		while(pt != vText.end())
		{
			pOverlay->remove2D(pt->getOverlayElement());
			pt++;
		}
		vText.clear();		
	}
}

ImageInfo* Displayable::addImage(const char* mtrName, int tx, int ty, int tw, int th)
{
	if(pOverlay == NULL)
	{
		Ogre::OverlayManager& overlayManager = Ogre::OverlayManager::getSingleton();
		
		static int in = 0;
		in++;
		char buf[256];
		sprintf(buf, "BaseUI::pOverlay %d", in);
		pOverlay = overlayManager.create(buf);

		pOverlay->show();
	}

	ImageInfo img;
	vImageInfo.push_back(img);
	vImageInfo.back().load(pOverlay, mtrName, tx, ty, tw, th);
	vImageInfo.back().updatePos(x ,y);
	return &(vImageInfo.back());
}

void Displayable::updateImagePos()
{
	V_IMAGE_INFO::iterator pi = vImageInfo.begin();
	while(pi != vImageInfo.end())
	{
		pi->updatePos(x, y);
		++pi;
	}

	V_TEXT::iterator pm = vText.begin();
	while(pm != vText.end())
	{
		pm->updatePos(x, y);
		++pm;
	}
}

void Displayable::draw(HDC hdc)
{
	Rectangle(hdc, (int)x, (int)y, (int)(x+w), (int)(y+h));
}

int Displayable::beClicked()
{
	return TYPE_BASE;
}

bool Displayable::isInArea(int mx, int my)
{
	return mx > x && my > y && mx < x+w && my < y+h;
}

bool Displayable::needCheck()
{
	return bCheck;
}

bool Displayable::canClick()
{
	return bClick;
}

float Displayable::getX()
{
	return x;
}

float Displayable::getY()
{
	return y;
}

void Displayable::setPos(float _x, float _y)
{
	x=_x; y=_y;
	if(pOverlay != NULL)
	{
		updateImagePos();
	}
}